package me.wangqu.weiyouqu.web.rest.business.dao.impl;

import me.wangqu.weiyouqu.web.rest.business.dao.ArticleDao;
import me.wangqu.weiyouqu.web.rest.business.model.BaseModel;
import me.wangqu.weiyouqu.web.rest.business.model.ListModel;
import me.wangqu.weiyouqu.web.rest.util.UrlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by joln on 2016/7/11.
 */
@Repository
public class ArticleImpl implements ArticleDao {

    private final Logger log = LoggerFactory.getLogger(ArticleImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private ListModel listModels = null;

    @Override
    public ListModel getCodeArticle(Integer code, Integer type, Pageable pageable) {
        if (!ObjectUtils.isEmpty(code)) {
            StringBuffer sql = new StringBuffer(128);
            List<Object> params = new ArrayList<>();
            sql.append("SELECT w.id,w.title,w.pic,w.day as date,l.link ,i.type from article w,link l,imgtype i where i.flag=? and w.type=? and w.code=? and w.top>? and w.flag=? ORDER BY w.top,w.id DESC LIMIT ?,?  ");
            params.add("small");
            params.add(type);
            params.add(code);
            params.add(-1);
            params.add(0);
            params.add(pageable.getOffset());
            params.add(pageable.getPageSize());
            List<BaseModel> articles = jdbcTemplate.query(sql.toString(), params.toArray(), new RowMapper<BaseModel>() {
                        @Override
                        public BaseModel mapRow(ResultSet rs, int rowNum) throws SQLException {
                            BaseModel articleModel = new BaseModel();
                            articleModel.setId(rs.getInt("id"));
                            articleModel.setTitle(rs.getString("title"));
                            articleModel.setDate(rs.getString("date"));
                            String link = rs.getString("link");
                            UrlUtil utils = new UrlUtil();
                            if (null != utils.getImg(rs.getString("pic"), rs.getString("i.type"))) {
                                articleModel.setImg_arr(utils.getImg(rs.getString("pic"), rs.getString("i.type")));
                            }
                            articleModel.setUrl(UrlUtil.getUrl(link, rs.getString("date"), rs.getInt("id"), type, code));
                            return articleModel;
                        }
                    }
            );


            return getListModel(articles);
        }

        return null;
    }


    private ListModel getListModel(List<BaseModel> bases) {
        listModels = new ListModel();
        listModels.setLists(bases);
        if (null != bases && !bases.isEmpty()) {
            listModels.setListNums(bases.size());
        }
        return listModels;
    }


    @Override
    public ListModel getCodeArticleHead(Integer code, Integer type) {
        if (!ObjectUtils.isEmpty(code)) {
            StringBuffer sql = new StringBuffer(128);
            List<Object> params = new ArrayList<>();
            sql.append("SELECT w.id,w.title,w.pic,w.day as date,l.link,i.type from article w,link l,imgtype i where i.flag=? and w.type=? and w.code=? and w.top>? and w.flag=? order by w.day desc ");
            params.add("banner");
            params.add(type);
            params.add(code);
            params.add(-1);
            params.add(1);
            List<BaseModel> heads = jdbcTemplate.query(sql.toString(), params.toArray(), new RowMapper<BaseModel>() {
                @Override
                public BaseModel mapRow(ResultSet rs, int rowNum) throws SQLException {
                    BaseModel head = new BaseModel();
                    head.setId(rs.getInt("id"));
                    head.setTitle(rs.getString("title"));
                    head.setDate(rs.getString("date"));
                    String link = rs.getString("link");
                    head.setUrl(UrlUtil.getUrl(link, rs.getString("date"), rs.getInt("id"), type, code));
                    UrlUtil utils = new UrlUtil();
                    if (null != utils.getImg(rs.getString("pic"), rs.getString("type"))) {
                        head.setImg_arr(utils.getImg(rs.getString("pic"), rs.getString("type")));
                    }
                    return head;
                }
            });


            return getListModel(heads);
        }

        return null;
    }

    @Override
    public ListModel getRecommend(Pageable pageable) {
        StringBuffer sql = new StringBuffer(128);
        List<Object> params = new ArrayList<>();
        sql.append("SELECT w.id,w.title,w.pic,w.day as date,w.type,w.code,l.link ,i.type from article,link l,imgtype i w where i.flag=? w.top>? and flag=? ORDER BY w.day DESC,w.id DESC LIMIT ?,?   ");
        params.add("small");
        params.add(-1);
        params.add(3);
        params.add(pageable.getOffset());
        params.add(pageable.getPageSize());
        List<BaseModel> baseModels = jdbcTemplate.query(sql.toString(), params.toArray(), new RowMapper<BaseModel>() {
            @Override
            public BaseModel mapRow(ResultSet rs, int rowNum) throws SQLException {
                BaseModel articleModel = new BaseModel();
                articleModel.setId(rs.getInt("id"));
                articleModel.setTitle(rs.getString("title"));
                articleModel.setDate(rs.getString("date"));
                articleModel.setType(rs.getInt("w.type"));
                String link = rs.getString("link");
                articleModel.setUrl(UrlUtil.getUrl(link, rs.getString("date"), rs.getInt("id"), rs.getInt("w.type"), rs.getInt("code")));
                UrlUtil utils = new UrlUtil();
                if (null != utils.getImg(rs.getString("pic"), rs.getString("i.type")))
                    articleModel.setImg_arr(utils.getImg(rs.getString("pic"), rs.getString("i.type")));
                return articleModel;

            }
        });
        return getListModel(baseModels);


    }


}
